<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    title="请选择要下载的年份"
    :maskClosable="false"
    @ok="handleSubmit"
    :keyboard="true"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { ActionEnum, VALIDATE_API } from '/@/enums/commonEnum';
  import { getValidateRules } from '/@/api/lamp/common/formValidateService';
  import { Api, save, update } from '/@/api/devOperation/tenant/datasourceConfig';
  import { searchFormSchemas } from './defUser.data';
  import { IddownLoad } from '/@/api/basic/qimo/index';
  import { downloadFiles } from '/@/utils/lamp/common';
  export default defineComponent({
    name: 'DatasourceConfigEdit',
    components: { BasicModal, BasicForm },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const { t } = useI18n();
      const type = ref(ActionEnum.ADD);
      const { createMessage } = useMessage();
      const [registerForm, { setFieldsValue, resetFields, updateSchema, validate, resetSchema }] =
        useForm({
          labelWidth: 100,
          schemas: searchFormSchemas(),
          showActionButtonGroup: false,
          actionColOptions: {
            span: 23,
          },
          baseColProps: { span: 24 },
        });

      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        await resetSchema(searchFormSchemas());
        await resetFields();
        // setModalProps({ confirmLoading: false });
        // type.value = data?.type;

        // if (unref(type) !== ActionEnum.ADD) {
        //   // 赋值
        //   const record = { ...data?.record };
        //   await setFieldsValue(record);
        // }

        // if (unref(type) !== ActionEnum.VIEW) {
        //   let validateApi = Api[VALIDATE_API[unref(type)]];
        //   await getValidateRules(validateApi, []).then(async (rules) => {
        //     rules && rules.length > 0 && (await updateSchema(rules));
        //   });
        // }
      });

      async function handleSubmit() {
        try {
          setModalProps({ confirmLoading: true });
          const params = await validate();
          const arr = String(params.year);
          const response = await IddownLoad([arr]);
          // console.log(response, '11111111111');
          downloadFiles(response);
          // console.log(arrs, '1111');
          // window.location.href = arrs;
          // if (unref(type) !== ActionEnum.VIEW) {
          //   if (unref(type) === ActionEnum.EDIT) {
          //     await update(params);
          //   } else {
          //     params.id = null;
          //     await save(params);
          //   }
          //   createMessage.success(t(`common.tips.${type.value}Success`));
          // }

          closeModal();
          emit('success');
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return { t, type, registerModal, registerForm, handleSubmit };
    },
  });
</script>
